package club.janna.diveinspringboot.autoconfigure.bootstrap;

import club.janna.diveinspringboot.autoconfigure.condition.ConditionalOnSystemProperty;
import club.janna.diveinspringboot.autoconfigure.service.CalculateService;
import club.janna.diveinspringboot.autoconfigure.service.impl.Java7CaclulateServiceImpl;
import club.janna.diveinspringboot.autoconfigure.service.impl.Java8CalculateServiceImpl;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

public class ConditionalBootstrap {

    @Bean
    @ConditionalOnSystemProperty(name="user.name", value = "guopanbo1")
    public CalculateService java7CalculateService() {
        System.out.println("test bean 1");
        return new Java7CaclulateServiceImpl();
    }
    @Bean
    @ConditionalOnMissingBean(CalculateService.class)
    public CalculateService java8CalculateService() {
        System.out.println("test bean 2");
        return new Java8CalculateServiceImpl();
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionalBootstrap.class)
                .web(WebApplicationType.NONE)
                .run(args);
        CalculateService service = context.getBean(CalculateService.class);
        System.out.println(service.sum(1,2));
    }
}
